<?php 
defined('IN_DESTOON') or exit('Access Denied');
if($DT_BOT || $_POST) dhttp(403);
require DT_ROOT.'/module/'.$module.'/common.inc.php';
if(!check_group($_groupid, $MOD['group_search'])) include load('403.inc');
require DT_ROOT.'/include/post.func.php';
include load('search.lang');
$CP = $MOD['cat_property'] && $catid && $CAT['property'];
if(!$areaid && $cityid && strpos($DT_URL, 'areaid') === false) {
	$areaid = $cityid;
	$ARE = $AREA[$cityid];
}
$sfields = array($L['by_title'], $L['by_content']);
$dfields = array('title', 'content');
$sorder  = array($L['order'], $L['order_auto'], $L['order_addtime'], $L['order_hits']);
$dorder  = array($MOD['order'], '', 'addtime DESC', 'hits DESC');
if(!$MOD['fulltext']) unset($sfields[2], $dfields[2]);
isset($fields) && isset($dfields[$fields]) or $fields = 0;
isset($order) && isset($dorder[$order]) or $order = 0;
$category_select = category_select('catid', $L['all_category'], $catid, $moduleid);
$area_select = $DT['city'] ? ajax_area_select('areaid', $L['all_area'], $areaid) : '';
$order_select  = dselect($sorder, 'order', '', $order);
$tags = array();
if($DT_QST) {
        /* 
         * commented by ujh 2016.08.10
         * 
         * 
	if($kw) {
            if(strlen($kw) < $DT['min_kw'] || strlen($kw) > $DT['max_kw']) 
                message(lang($L['word_limit'], array($DT['min_kw'], $DT['max_kw'])), $MOD['linkurl'].'search.php');

            if($DT['search_limit'] && $page == 1) {
                if(($DT_TIME - $DT['search_limit']) < get_cookie('last_search')) 
                    message(lang($L['time_limit'], array($DT['search_limit'])), $MOD['linkurl'].'search.php');

                set_cookie('last_search', $DT_TIME);
            }
	}
         * 
         */

	$pptsql = '';
	if($CP) {
		require DT_ROOT.'/include/property.func.php';
		$PPT = property_condition($catid);
		foreach($PPT as $k=>$v) {
			$PPT[$k]['select'] = '';
			$oid = $v['oid'];
			$tmp = 'ppt_'.$oid;
			if(isset($$tmp)) {
				$PPT[$k]['select'] = $tmp = $$tmp;
				if($tmp && in_array($tmp, $v['options'])) {
					$tmp = 'O'.$oid.':'.$tmp.';';
					$pptsql .= " AND pptword LIKE '%$tmp%'";
				}
			}
		}
	}
	$fds = $MOD['fields'];
	$condition = '';
        /* inserted by ujh 2016.08.04 -- begin -- */
        // $zpcategoryid - 招标分类
        // $categoryid  - 行业分类
        // $areaid - 地区
        // $b_date - 日期        
        if( !empty($zpcategoryid) )
        { 
            $catid = $zpcategoryid;
            if( !empty($CAT['child']) )
            {
                if( empty($CAT['arrchildid']) )
                {
                    $CAT['arrchildid'] = $zpcategoryid;
                }
                else
                {
                    $CAT['arrchildid'] = $CAT['arrchildid'] . "," . $zpcategoryid;
                }
            }
        }
        
        if( !empty($categoryid))
        {
            $result_cateogry = $db->query("SELECT * FROM zb_filtercategory WHERE fid  IN ($categoryid,0) ");
            
            $_categoryObj = array();
            while($r = $db->fetch_array($result_cateogry)) {
                $_categoryObj[] = $r;
            }
            
            if( !empty($_categoryObj))
            {
                $f_cat_lg = $_categoryObj[0]['f_cat_lg'];                
                $result_cateogry = $db->query("SELECT * FROM zb_filtercategory WHERE f_cat_sm != 0 AND f_cat_lg=$f_cat_lg");
                while($r = $db->fetch_array($result_cateogry)) {
                    $_categoryObj[] = $r;
                }
            }
            
            if( !empty( $_categoryObj ) )
            {
                $condition .= " AND ( ";
                $first = true;
                
                foreach( $_categoryObj as $_catObj )
                {
                    // $condition .= " AND ( LENGTH(filtercategory) = 0 OR filtercategory LIKE '%(0)%' OR filtercategory LIKE '%(". $_catObj['fid'] .")%' )";
                    if( $first == false )
                    {
                        $condition .= " OR ";
                    }
                    $condition .= " filtercategory LIKE '%(". $_catObj['fid'] .")%' ";
                    $first = false;
                }
                $condition .= " ) ";
            }
        }
        
        if( !empty($areaid) )
        {
            $condition .= " AND ( LENGTH(filteraddress) = 0 OR filteraddress LIKE '%(0)%' OR filteraddress LIKE '%($areaid)%' )";
            $areaid = 0;
        }
        
        if( !empty($b_date) )
        {
            $now = time();
            $begintime = $now;
            $endtime = $now;
        
            $year = date("Y", $now);
            $month = date("m", $now);
            $day = date("d", $now);
            
            //$todayDate = DateTime::setDate ( $year, $month, $day );            
            //$endtime = mktime(23, 59, 59, $month, $day, $year);            
            //$addtime = 0;
            //$addDate = new DateInterval('P0D');
            if( strcmp($b_date, "week") == 0 ) // 一周
            {
                $addtime = 3600 * 24 * 7;
                $begintime = $now - $addtime;
                $endtime = $now;
            }
            else if( strcmp($b_date, "day") == 0 ) // 一天
            {
                $addtime = 3600 * 24 ;
                $begintime = $now - $addtime;
                $endtime = $now;
            }
            else if( strcmp($b_date, "month") == 0 ) // 近一月
            {
                $addtime = 3600 * 24 * 30 ;
                $begintime = $now - $addtime;
                $endtime = $now;
            }
            else if( strcmp($b_date, "quarter") == 0 ) // 近一月
            {
                $addtime = 3600 * 24 * 30 * 3 ;
                $begintime = $now - $addtime;
                $endtime = $now;
            }
            else if( strcmp($b_date, "6month") == 0 ) // 半年
            {
                $addtime = 3600 * 24 * 30 * 6 ;
                $begintime = $now - $addtime;
                $endtime = $now;
            }
            else if( strcmp($b_date, "year") == 0 ) // 一年
            {
                $addtime = 3600 * 24 * 365 ;
                $begintime = $now - $addtime;
                $endtime = $now;
            }
            else 
            {
                $paramDate = date_create_from_format('Y-m-d', $b_date);
                $paramDate = $paramDate->setTime(0, 0, 0);
                $begintime = $paramDate->getTimestamp();
                
                $paramDate = $paramDate->setTime(23, 59, 59);                
                $endtime =  $paramDate->getTimestamp();
            }
            $condition .= " AND addtime > " . $begintime . " AND addtime < " . $endtime ;
        }
        
        /* inserted by ujh 2016.08.04 -- end -- */
        
        /* inserted by ujh 2016.08.06 -- begin -- */
        $displaySearchPanel = "";        
        if( empty($searchtype) || $searchtype == 0)
        {
            $displaySearchPanel = "display:none";
        }
        /* inserted by ujh 2016.08.06 -- end -- */
        
        
        /* inserted by ujh 2016.09.14 -- begin -- */
        
        if( empty($areaid2) )
            $areaid2 = "(0)";
        
        if( empty($zpcategory2) )
            $zpcategory2 = "(0)";
        
        if( empty($category2) )
            $category2 = "(0)";
        
        
        // $t_condition = $condition;
        // areas
        $user_areas = array();
        foreach($AREA as $area ) { 
            if( $area['parentid'] == '0' )
                $user_areas[] = $area;
        }
        
        $search_areaid= $areaid2;
        $search_areatext = '';
        foreach($user_areas as &$t1)
        {
            $searchText = "(" . $t1['areaid'] . ")";
            
            $t1['checked'] = '';
            if( !empty($search_areaid))                
            {
                $iPos =  mb_strpos ( $search_areaid , $searchText  );                
                if( $iPos  !== false )
                {
                    $t1['checked'] = 'checked';
                    $search_areatext .= $searchText . ",";
                }
                else
                {
                    $t1['checked'] = '';
                }
            }
          /*  else
            {
                $t1['checked'] = 'checked';
                $search_areatext .=  $searchText . ",";
            }
           * 
           */
        } 
        
        $searchText = "(0)";
        $area0_checked = "";
        $iPos =  mb_strpos ( $search_areaid, $searchText  );                
        if( $iPos  !== false )
        {
            $area0_checked = 'checked="checked"';            
        }
        
        
        
        // filter categories
        $catef = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 0 as ishide, f_cat_lg FROM {$DT_PRE}filtercategory WHERE f_cat_sm=0 order by displayorder asc");
        $fenleis = array();
        while($cf = $db->fetch_array($catef)){
           /*
            $catez = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 0 as ishide FROM {$DT_PRE}filtercategory WHERE f_cat_sm!=0 and f_cat_lg=" . $cf['f_cat_lg'] . " order by displayorder asc");
            while($cz = $db->fetch_array($catez)){
                $cf['child'][] = $cz;
            }*/
            $fenleis[] = $cf;
        }
        $user_fcategories = $fenleis;
        
        $search_filtercategory= $category2;
        $search_filtercategorytext = '';
        foreach($user_fcategories as &$t3)
        {
            $t3_checked = false;
            foreach($t3['child'] as &$t4)
            {                
                $searchText = "(" . $t4['id'] . ")";            
                $t4['checked'] = '';
                if( !empty($search_filtercategory))                
                {
                    $iPos =  mb_strpos ( $search_filtercategory , $searchText  );                
                    if( $iPos  !== false )
                    {
                        $t4['checked'] = 'checked';
                        $t3_checked = true;
                        $search_filtercategorytext .= $searchText . ",";
                    }
                    else
                    {
                        $t4['checked'] = '';
                    }
                }
                /*
                else
                {
                    $t4['checked'] = 'checked';
                    $search_filtercategorytext .=  $searchText . ",";
                    $t3_checked = true;
                }
                 */
            }
            
            if( $t3_checked == true )
            {
                $searchText = "(" . $t3['id'] . ")";
                $search_filtercategorytext .=  $searchText . ",";
                $t3['checked'] = 'checked';
            }
            else
            {
                $searchText = "(" . $t3['id'] . ")";
                $t3['checked'] = '';
                if( !empty($search_filtercategory))                
                {
                    $iPos =  mb_strpos ( $search_filtercategory , $searchText  );                
                    if( $iPos  !== false )
                    {
                        $t3['checked'] = 'checked';
                        $search_filtercategorytext .= $searchText . ",";
                        $t3_checked = true;
                    }
                    else
                    {
                        $t3['checked'] = '';
                    }
                }
                /*
                else
                {
                    $t3['checked'] = 'checked';
                    $search_filtercategorytext .=  $searchText . ",";
                }
                */
            }
        } 
        
        
        $searchText = "(0)";
        $fcategory0_checked = "";
        $iPos =  mb_strpos ( $search_filtercategory, $searchText  );                
        if( $iPos  !== false )
        {
            $fcategory0_checked = 'checked="checked"';            
        }
        
        // categorys
        $user_categorys = $TOPCAT ;
       
        $search_zpcategory= $zpcategory2;
        $search_zpcategorytext = '';
        foreach($user_categorys as &$t2)
        {
            $searchText = "(" . $t2['catid'] . ")";
            
            $t2['checked'] = '';
            if( !empty($search_zpcategory))                
            {
                $iPos =  mb_strpos ( $search_zpcategory , $searchText  );                
                if( $iPos  !== false )
                {
                    $t2['checked'] = 'checked';
                    $search_zpcategorytext .= $searchText . ",";
                }
                else
                {
                    $t2['checked'] = '';
                }
            }
            /*
            else
            {
                $t2['checked'] = 'checked';
                $search_zpcategorytext .=  $searchText . ",";
            }
             */
        } 
        
           
        $searchText = "(0)";
        $category0_checked = "";
        $iPos =  mb_strpos ( $search_zpcategory, $searchText  );                
        if( $iPos  !== false )
        {
            $category0_checked = 'checked="checked"';            
        }
        
       // $condition = ' 1 ';
        $t_areas = "";
        $t_filters = "";
        $t_categories = "";
        if( !empty($search_areatext) )
            $t_areas = explode( "," , $search_areatext );
        if( !empty($search_filtercategorytext) )
            $t_filters = explode( "," , $search_filtercategorytext );
        if( !empty($search_zpcategorytext) )
            $t_categories = explode( "," , $search_zpcategorytext );        
        
        if( !empty( $t_areas ) )
        {
            $condition .= " AND ( LENGTH(filteraddress) = 0 OR filteraddress LIKE '%(0)%' OR ";
            $first = true;

            foreach( $t_areas as $t_area )
            {
                if( !empty( $t_area) )
                {
                    if( $first == false )
                        $condition .= " OR ";

                    $condition .= " filteraddress LIKE '%". $t_area ."%' ";
                    $first = false;
                }
            }
            $condition .= " ) ";
        }
        
        if( !empty( $t_filters ) )
        {
            $condition .= " AND ( LENGTH(filtercategory) = 0 OR filtercategory LIKE '%(0)%' OR ";
            $first = true;

            foreach( $t_filters as $t_filter )
            {
                if( !empty( $t_filter ) )
                {
                    if( $first == false )
                        $condition .= " OR ";

                    $condition .= " filtercategory LIKE '%". $t_filter ."%' ";
                    $first = false;
                }
            }
            $condition .= " ) ";
        }
                
        if( !empty( $t_categories ) )
        {
            $condition .= " AND ( ";
            $first = true;

            foreach( $t_categories as $t_category )
            {
                if( !empty( $t_category ) )
                {
                    if( $first == false )
                        $condition .= " OR ";

                    $replacestr = array("(", ")" );
                    $t_category = str_replace($replacestr, "", $t_category);

                    $condition .= " catid = " . $t_category . " ";
                    $first = false;
                }
            }
            $condition .= " ) ";
        }
        /*
        $pagesize = 15;
	$offset = ($page-1)*$pagesize;
	$items = $db->count( "zb_article_23", $condition );
	$pages = pages($items, $page, $pagesize);
        $zhaobiao_url = $MODULE[23]['linkurl']; // zhaobiao        
        $datetype = 5;
        $showpage = true;
        
        if($items) {
            $order = " ORDER BY addtime desc  ";
            $fields = "itemid,title,thumb,linkurl,style,catid,introduce,addtime,edittime,username,islink,areaid,filteraddress, filtercategory";
            $result = $db->query("SELECT {$fields} FROM zb_article_23 WHERE {$condition}{$order} LIMIT {$offset},{$pagesize}");

            $index = 1;
            while($r = $db->fetch_array($result)) {
                    $r['adddate'] = timetodate($r['addtime'], 5);
                    $r['editdate'] = timetodate($r['edittime'], 5);
                    if($lazy && isset($r['thumb']) && $r['thumb']) $r['thumb'] = DT_SKIN.'image/lazy.gif" original="'.$r['thumb'];
                    $r['alt'] = $r['title'];
                    $r['title'] = set_style($r['title'], $r['style']);
                    if($kw) $r['title'] = str_replace($replacef, $replacet, $r['title']);
                    if(!$r['islink']) $r['linkurl'] = $zhaobiao_url.$r['linkurl'];
                    $r['itemno'] = $index + $offset;
                    $index = $index + 1;
                    $tags[] = $r;
            }
            $db->free_result($result);
	}
        */
        /* inserted by ujh 2016.09.14 -- end -- */
        
        
        
        
        
        
        
        
        
        // $condition = $t_condition;
        
	if($catid) $condition .= ($CAT['child']) ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
	if($areaid) $condition .= ($ARE['child']) ? " AND areaid IN (".$ARE['arrchildid'].")" : " AND areaid=$areaid";
	if($dfields[$fields] == 'content') {
		if($keyword && $MOD['fulltext'] == 1) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
		$condition = str_replace('AND ', 'AND i.', $condition);
		$condition = str_replace('i.content', 'd.content', $condition);
		$condition = "i.status=3 AND i.itemid=d.itemid".$condition;
		if($keyword && $MOD['fulltext'] == 2) $condition .= " AND MATCH(`content`) AGAINST('$kw'".(preg_match("/[+-<>()~*]/", $kw) ? ' IN BOOLEAN MODE' : '').")";
		$table = $table.' i,'.$table_data.' d';
		$fds = 'i.'.str_replace(',', ',i.', $fds);
	} else {
		if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
		if($pptsql) $condition .= $pptsql;//PPT
		$condition = "status=3".$condition;
	}
	$pagesize = $MOD['pagesize'];
	$offset = ($page-1)*$pagesize;
	//echo $condition;
	$items = $db->count($table, $condition, $DT['cache_search']);
	$pages = pages($items, $page, $pagesize);
	if($items) {
		$order = $dorder[$order] ? " ORDER BY $dorder[$order]" : '';
		$result = $db->query("SELECT $fds FROM {$table} WHERE {$condition}{$order} LIMIT {$offset},{$pagesize}", $DT['cache_search'] && $page == 1 ? 'CACHE' : '', $DT['cache_search']);
		if($kw) {
			$replacef = explode(' ', $kw);
			$replacet = array_map('highlight', $replacef);
		}
                
                $index = 1;
		while($r = $db->fetch_array($result)) {
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			if($lazy && isset($r['thumb']) && $r['thumb']) $r['thumb'] = DT_SKIN.'image/lazy.gif" original="'.$r['thumb'];
			$r['alt'] = $r['title'];
			$r['title'] = set_style($r['title'], $r['style']);
			if($kw) $r['title'] = str_replace($replacef, $replacet, $r['title']);
			if(empty($r['filteraddress'])) $r['filteraddress'] = '全国';
			if(empty($r['filtercategory'])) $r['filtercategory'] = '全行业';
			if(!$r['islink']) $r['linkurl'] = $MOD['linkurl'].$r['linkurl'];
                        $r['itemno'] = $index + $offset;
                        $index = $index + 1;
			$tags[] = $r;
		}
		$db->free_result($result);
		if($page == 1 && $kw) keyword($kw, $items, $moduleid);
	}
}
$showpage = 1;
$datetype = 5;
$target = '_blank';
$cols = 5;
$class = '';
$seo_file = 'search';
include DT_ROOT.'/include/seo.inc.php';
if($EXT['mobile_enable']) $head_mobile = $EXT['mobile_url'].($kw ? 'index.php?moduleid='.$moduleid.'&kw='.encrypt($kw, DT_KEY.'KW') : 'search.php?action=mod'.$moduleid);
include template($MOD['template_search'] ? $MOD['template_search'] : 'search', $module);
?>